//
//  DealAllViewController.m
//  HengYouCai
//
//  Created by ycgwl on 2017/5/3.
//  Copyright © 2017年 lwj. All rights reserved.
//

#import "DealAllViewController.h"

@interface DealAllViewController ()
{
    NSInteger _pageNo;
}
/**
 数据源
 */
@property (nonatomic, retain)NSMutableArray *totalArr;

@end

@implementation DealAllViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.backgroundColor = Normal_BackColor;
    self.tableView.showsVerticalScrollIndicator = NO;
    _pageNo = 1;
    [self.view showHUDLoading];
    [self netWorking];
    [self startRefresh];
}
#pragma mark- 刷新
- (void)startRefresh
{
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        _pageNo = 1;
        [self netWorking];
        
    }];
    
    
}
#pragma tableviewDatasource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 75;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _totalArr.count;
}
#pragma tableviewDelegate

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = _totalArr[indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"dealCellID"];
    UILabel *titleLab = [cell viewWithTag:1001];
    UILabel *timeLab = [cell viewWithTag:1002];
    UILabel *amount = [cell viewWithTag:1003];

    titleLab.text = dic[@"title"];
    timeLab.text = dic[@"time"];
    NSString *amountStr = dic[@"amount"];
    if ([amountStr hasPrefix:@"+"]) {
        amount.textColor = [UIColor colorWithHexString:@"5db36b"];
    }else{
        amount.textColor = Normal_RedColor;

    }
    amount.text = amountStr;

    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)netWorking
{
    WLGetWeakSelf(weakSelf, self);
    NSLog(@"-----%@",self.statusType);
    
    [HYCNetService myWalletTradeDetailListWithDict:@{@"tradeType":[NSString stringWithFormat:@"%@",self.statusType],
                                                     @"pageNo":[NSString stringWithFormat:@"%ld",_pageNo],
                                                     @"pageSize":@"10"}
                                         onSuccess:^(NSDictionary *dictResult) {
                                             //
                                             [weakSelf.view hideHUDLoading:0];
                                             weakSelf.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
                                                 
                                                 [weakSelf netWorking];
                                             }];
                                             NSArray *lists = dictResult[@"tradeDetailList"];
                                             if (_pageNo==1) {
                                                 [_totalArr removeAllObjects];
                                                 [weakSelf.tableView.mj_header endRefreshing];
                                             }else{
                                                 [weakSelf.tableView.mj_footer endRefreshing];
                                                 
                                             }
                                             _pageNo++;
                                             weakSelf.tableView.mj_footer.hidden = lists.count<10;
                                             
                                             [weakSelf.totalArr addObjectsFromArray:lists];
                                             [weakSelf.tableView reloadData];

        
    } andFailed:^(NSInteger code, NSString *errorMsg) {
        //
        [weakSelf.view showHUDFailTips:errorMsg hideDelay:kHUDHideDelay];
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf.tableView.mj_footer endRefreshing];
    }];
}
- (NSMutableArray *)totalArr
{
    if (!_totalArr) {
        _totalArr = [NSMutableArray array];
    }
    return _totalArr;
}
@end
